package com.xiaochong.bitong.api.service;

import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.api.config.H5Url;
import com.xiaochong.bitong.api.constant.ReturnCode;
import com.xiaochong.bitong.api.utils.StringUtil;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.tir.common.data.mapper.KnowledgeRoomMapper;
import com.xiaochong.tir.common.data.mapper.SyChannelsMapper;
import com.xiaochong.tir.common.data.mapper.SyResearchReportFileMapper;
import com.xiaochong.tir.common.data.mapper.SyResearchReportMapper;
import com.xiaochong.tir.common.data.po.KnowledgeRoom;
import com.xiaochong.tir.common.data.po.SyChannels;
import com.xiaochong.tir.common.data.po.SyResearchReport;
import com.xiaochong.tir.common.data.po.SyResearchReportFile;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationVo;
import com.xiaochong.tir.common.util.ApiResultCodeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 此类为[基础代码]自动生成,已经拥有基本的增删改成操作 
 * <其他请自行扩展>
 * @author mengxiangdong
 * @time 2018-05-23 14:07
 *
 */
@Service
public class KnowledgeService {

    @Autowired
    private KnowledgeRoomMapper knowledgeRoomMapper;

    @Autowired
    private OSSTool ossTool;

    /**
     *
     知识讲堂
     * @return List<T>
     */
    public ApiResultVo knowledgeRoomList(CurrencyQuotationVo currencyQuotationVo){
        ApiResultVo resultVo=new ApiResultVo();
        resultVo.setTradeId( LogTrace.getTrace().getTraceId() );
        try {
            KnowledgeRoom knowledgeRoom = new KnowledgeRoom();
            knowledgeRoom.setEnableFlag( true );
            List<KnowledgeRoom>  KnowledgeRoomList = knowledgeRoomMapper.selectByEntityList2( knowledgeRoom );
            List<KnowledgeRoom>  KnowledgeRoomList2 = new ArrayList<>(  );
            for(int i=0;i<KnowledgeRoomList.size();i++){
                KnowledgeRoom knowledgeRoom1 = KnowledgeRoomList.get( i );
                if(knowledgeRoom1.getPublishMode().equals( "1" )){
                    KnowledgeRoomList2.add( knowledgeRoom1 );
                }
                if(knowledgeRoom1.getPublishMode().equals( "2" ) && knowledgeRoom1.getPublishTime().getTime()>=new Date(  ).getTime()){
                    KnowledgeRoomList2.add( knowledgeRoom1 );
                }
            }

            for (KnowledgeRoom k : KnowledgeRoomList2) {
                k.setUrl(H5Url.getKnowledge()+k.getId());
                if(StringUtils.isNotBlank(k.getArticleImg())){
                    if(!k.getArticleImg().contains( "http" )){
                        String imgUrl=ossTool.getOSSUrl( k.getArticleImg() );
                        k.setArticleImg( imgUrl );
                    }
                }
            }

            resultVo.setCode( ReturnCode.SUCCESS);
            resultVo.setData(KnowledgeRoomList2);
            resultVo.setTotal(KnowledgeRoomList2.size());
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("知识讲堂",e);
            resultVo.setCode(ReturnCode.ERROR);
        }
        return resultVo;
    }

}